<?php 
namespace Emos\Field;
use Emos\Common;
use Emos\Db;

/** 
Basic class for form fields.
Mostly you have to override only getHtml() method for changing field look and feel.
*/
abstract class AbstractControl extends AbstractItem {
    public $db;
    
    /**  Error message or null if no errors. */
    protected $error = null;

    /** Label for the field or null if no label required */
    var $label = null;

    /** Name of DB field */
    var $dbName;

    /** Name of HTML field */
    var $htmlName;

    /** Shows if fields can be nullable */
    var $nullable = false;
        
    /** Label of the field in the same representation as it can be fetched from database  */
    var $value = null;

    public function setNullable($nullable=true) {
        $this->nullable = $nullable;
        return $this;
    }    
   
    /** Return HTML code of control with values in it */
    public function getHtml() {
        $html = '';
        
        if($this->nullable) {
            $checked = ($this->isNull()) ? 'checked' : '';
            $html .= "<div class='nuller'><input type='checkbox' name='{$this->htmlName}_isNull' value='null' {$checked} class='null'> ".Common::t('null')."</div>";
            
            $display = ($this->isNull()) ? 'none' : 'block';
            $html .= "<div class='nullableControl' style='display:{$display}'>";
        }        
        
        $html .= $this->html();

        if($this->nullable) {
            $html .= "</div>";
        }        
        
        return $html;
    }
    
    public function initName($dbName, $htmlName) {    
        $this->dbName = $dbName;
        $this->htmlName = $htmlName;
    }
    
    /** Validates post. Sets error message into $error variable if nessesary  */
    public function validate() {
        $this->error = null;
        
        return true;
    }    
    
    public function isNullPost() {
        return (@$_POST["{$this->htmlName}_isNull"] === 'null');
    }
    
    protected function getPost() {
        if($this->nullable && $this->isNullPost()) {
            return null;
        }        
        return @$_POST[$this->htmlName];
    }
    
    protected function isNull() {
        return ($this->value === null);
    }

    public function setValueFromRecord(array $record) {
        $this->value = @$record[$this->dbName];
    }    
        
    public function insert() {        
        return $this->update();
    }
        
    public function update() {
        $newValue = $this->getPost();
        if($newValue === null) {
            return array($this->dbName => new Db\SqlExpression("NULL"));        
        }
        else {
            return array($this->dbName => $newValue);        
        }
    }
    
    public function isValid() {
        return ($this->error === null);
    }    
    
    public function getErrorMessage() {
        if($this->error === true) {
            return Common::t('incorrect value');
        }
        return $this->error;
    }
    
    
    
//    public function copy() {
//        $this->insert();
//    }      
        
    public function delete() {
    }             
        
    public function getDdlFields() {
        return array(
            $this->dbName => "VARCHAR(255)"
        );
    }
    
    public function getFake() {
        return null;
    }    
    
    protected function escapeSqlStr($str) {
        return $this->db->escape($str);
    }
}